/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.core;

import com.kurvykurvy.core.ui.BookLayoutTopComponent;
import com.kurvykurvy.core.ui.PageLayoutTemplateTopComponent;
import com.kurvykurvy.page.Book;
import com.kurvykurvy.page.BookTemplate;
import com.kurvykurvy.page.ImageFrameTemplate;
import com.kurvykurvy.page.PageLayoutTemplate;
import com.kurvykurvy.page.PageSpread;
import com.kurvykurvy.page.PageSpreadLayout;
import com.kurvykurvy.page.SimpleKurvyBook;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.openide.util.Exceptions;

/**
 *
 * @author HY
 */
public class KurvyDataManager
{

    private static KurvyDataManager DEFAULT_INSTANCE;

    static
    {
        DEFAULT_INSTANCE = new KurvyDataManager();
        debug();
    }
    Book currentBook;
    BookTemplate currentBookTemplate;

    private KurvyDataManager()
    {
    }

    public void setCurrentBook( Book book )
    {
        this.currentBook = book;
        BookLayoutTopComponent bookLayoutComponent = BookLayoutTopComponent.getDefault();
        if( bookLayoutComponent != null )
            bookLayoutComponent.setCurrentBook( this.currentBook );
    }

    public Book getCurrentBook()
    {
        return this.currentBook;
    }

    public void setCurrentBookTemplate( BookTemplate template )
    {
        this.currentBookTemplate = template;
    }

    public BookTemplate getCurrentBookTemplate()
    {
        return this.currentBookTemplate;
    }

    public static KurvyDataManager getInstance()
    {
        return DEFAULT_INSTANCE;
    }

    private static void debug()
    {
        try
        {
            BookTemplate bookTemplate = new BookTemplate();

            File maskFile = new File( "E:/kurvy_templete/bookshape-left-1.gif" );
            File templateFile = new File( "E:/kurvy_templete/a_left1-3.gif" );

            BufferedImage mask = ImageIO.read( maskFile );
            BufferedImage template = ImageIO.read( templateFile );

            ArrayList<ImageFrameTemplate> frameTemplates = new ArrayList<ImageFrameTemplate>();
            ImageFrameTemplate frameTemplate = new ImageFrameTemplate( new Point( 109, 204 ), new Point( 237, 400 ) );
            frameTemplate.setID( "FRAME1" );
            frameTemplates.add( frameTemplate );
            ImageFrameTemplate frameTemplate2 = new ImageFrameTemplate( new Point( 257, 204 ), new Point( 388, 400 ) );
            frameTemplate2.setID( "FRAME2" );
            frameTemplates.add( frameTemplate2 );
            PageLayoutTemplate pageTemplate = new PageLayoutTemplate( mask, template, frameTemplates, PageLayoutTemplate.LEFT_PAGE );
            bookTemplate.addPageTemplate( pageTemplate );

            maskFile = new File( "E:/kurvy_templete/bookshape-left-1.gif" );
            templateFile = new File( "E:/kurvy_templete/a-left3-3.gif" );

            mask = ImageIO.read( maskFile );
            template = ImageIO.read( templateFile );

            frameTemplates = new ArrayList<ImageFrameTemplate>();
//            frameTemplate = new ImageFrameTemplate( new Point( 109, 204 ), new Point( 237, 400 ) );
//            frameTemplate.setID( "FRAME1" );
//            frameTemplates.add( frameTemplate );
//            frameTemplate2 = new ImageFrameTemplate( new Point( 257, 204 ), new Point( 388, 400 ) );
//            frameTemplate2.setID( "FRAME2" );
//            frameTemplates.add( frameTemplate2 );
            pageTemplate = new PageLayoutTemplate( mask, template, frameTemplates, PageLayoutTemplate.LEFT_PAGE );
            bookTemplate.addPageTemplate( pageTemplate );

            maskFile = new File( "E:/kurvy_templete/bookshape-1.gif" );
            templateFile = new File( "E:/kurvy_templete/a_right1-3.gif" );

            mask = ImageIO.read( maskFile );
            template = ImageIO.read( templateFile );

            frameTemplates = new ArrayList<ImageFrameTemplate>();
            frameTemplate = new ImageFrameTemplate( new Point( 36, 204 ), new Point( 166, 400 ) );
            frameTemplate.setID( "FRAME1" );
            frameTemplates.add( frameTemplate );
            frameTemplate2 = new ImageFrameTemplate( new Point( 183, 204 ), new Point( 313, 400 ) );
            frameTemplate2.setID( "FRAME2" );
            frameTemplates.add( frameTemplate2 );
            pageTemplate = new PageLayoutTemplate( mask, template, frameTemplates, PageLayoutTemplate.RIGHT_PAGE );
            pageTemplate.setBackgroundOnly( true );
            bookTemplate.addPageTemplate( pageTemplate );

            maskFile = new File( "E:/kurvy_templete/bookshape-1.gif" );
            templateFile = new File( "E:/kurvy_templete/a-right3-3.gif" );

            mask = ImageIO.read( maskFile );
            template = ImageIO.read( templateFile );

            frameTemplates = new ArrayList<ImageFrameTemplate>();
            frameTemplate = new ImageFrameTemplate( new Point( 36, 204 ), new Point( 166, 400 ) );
            frameTemplate.setID( "FRAME1" );
            frameTemplates.add( frameTemplate );
            frameTemplate2 = new ImageFrameTemplate( new Point( 183, 204 ), new Point( 313, 400 ) );
            frameTemplate2.setID( "FRAME2" );
            frameTemplates.add( frameTemplate2 );
            pageTemplate = new PageLayoutTemplate( mask, template, frameTemplates, PageLayoutTemplate.RIGHT_PAGE );
            pageTemplate.setBackgroundOnly( true );
            bookTemplate.addPageTemplate( pageTemplate );

            BufferedImage fullPageMask = null;
            BufferedImage img = null;
            BufferedImage img2 = null;
            BufferedImage img3 = null;
            ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
            try
            {
                File fullPageMaskFile = new File( "E:/kurvy_templete/fullpage-new.gif" );
                img = ImageIO.read( new File( "E:/pics/RainerMirau.jpg" ) );
                images.add( img );
                img2 = ImageIO.read( new File( "E:/pics/IMG_0559.jpg" ) );
                images.add( img2 );
                img3 = ImageIO.read( new File( "E:/pics/IMG_0569.jpg" ) );
                images.add( img3 );

                fullPageMask = ImageIO.read( fullPageMaskFile );
                bookTemplate.setPageSpreadMask( fullPageMask );
            }
            catch ( IOException ex )
            {
                Exceptions.printStackTrace( ex );
            }

            PageSpreadLayout pageSpreadTemp = new PageSpread();
            pageSpreadTemp.setPageSpreadMask( bookTemplate.getPageSpreadMask() );
            pageSpreadTemp.setPageLayoutTemplate( bookTemplate.getLeftPageTemplate() );
            pageSpreadTemp.setPageLayoutTemplate( bookTemplate.getRightPageTemplate() );
            pageSpreadTemp.setImages( images );

            Book currentBook = new SimpleKurvyBook( bookTemplate );
            currentBook.addPageSpread( (PageSpread) pageSpreadTemp );

            DEFAULT_INSTANCE.setCurrentBook( currentBook );
            DEFAULT_INSTANCE.setCurrentBookTemplate( bookTemplate );

            PageLayoutTemplateTopComponent.getDefault().setBookTemplate( DEFAULT_INSTANCE.getCurrentBookTemplate() );
        }
        catch ( IOException ex )
        {
            Exceptions.printStackTrace( ex );
        }
    }
}
